/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package cn.icanci.message.service.impl;

import cn.icanci.message.service.SendSmsService;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;

/**
 * @author icanci
 * @date Created in 2021/04/27 17:59
 * @version 1.0
 */
@Slf4j
@Service
public class SendSmsServiceImpl implements SendSmsService {

    /**
     * 短信发送业务
     *
     * @param phoneNum     手机号
     * @param templateCode 模板Code
     * @param code         需要发送的信息
     * @return 返回是否发送成功
     */
    @Override
    public boolean send(String phoneNum, String templateCode, Map<String, Object> code) {
        /**
         * 连接阿里云
         */
        DefaultProfile profile = DefaultProfile.getProfile(
            // 不需要改
            "cn-hangzhou",
            // key
            "LTAI4G4JptDwzhS624rapUz2",
            // pwd
            "jKrNsVyG1jhYkWN5g6V5v3IzQZoGZS");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        // 构建请求
        request.setMethod(MethodType.POST);
        // 官方提供，不需要修改
        request.setDomain("dysmsapi.aliyuncs.com");
        // 官方提供，不需要修改 版本号
        request.setVersion("2017-05-25");
        // 不需要修改
        request.setAction("SendSms");
        // 自定义的参数 （手机号，验证码，签名，模板）
        // 自定义手机号
        request.putQueryParameter("PhoneNumbers", phoneNum);
        // 自定义签命
        request.putQueryParameter("SignName", "是小猪童鞋啦");
        // 自定义模板 Code
        request.putQueryParameter("TemplateCode", templateCode);
        // 自定义验证码
        request.putQueryParameter("TemplateParam", JSON.toJSONString(code));
        try {
            CommonResponse response = client.getCommonResponse(request);
            log.info("SendSmsService send code,{}",response.getData());
            // 是否成功
            return response.getHttpResponse().isSuccess();
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }
        return false;
    }
}
